;;;
;;; JaCoLiB
;;; Copyright (C) 2008 Alessandro Serra
;;; 
;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;; 
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;; 
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;;

;;;; Created on 2008-07-21 12:09:43

(in-package :jacolib)

;
; RPC Server
;

(defclass rpc-server-stub ()
  ((rpc-server
   :TYPE (or nil rpc-server)
   :INITARG :rpc-server
   :ACCESSOR rpc-server-stub-server)
   (callback-client-sub
    :TYPE rpc-client-stub
    :INITARG :callback-client-stub
    :ACCESSOR rpc-server-stub-callback-client-stub))
  (:documentation "A stub of a RPC server. 
It deserializes the requests received from a remote client, 
and then serializes the responses provided by a server."))

; functions   

(defun rpc-server-stub-exec (server-stub jsonstr)
  (declare (type rpc-server-stub server-stub)
           (string jsonstr))
  (let ((rpc-request (json-serializer-decode-from-string jsonstr)))
    (if (not (typep rpc-request 'rpc-request))
        (error 'protocol-error :message "Expected a rpc-request object."))
    (json-serializer-encode-to-string
     (rpc-server-call-method-handler (rpc-server-stub-server server-stub) rpc-request))))

; quiting

(defun rpc-server-stub-quit% (stub)
  (rpc-server-quit% (rpc-server-stub-server stub))
  (setf (slot-value stub 'rpc-server) nil)
  (setf (slot-value stub 'callback-client-sub) nil))
